1
|
|
|
import {CommandHandler} from '@nestjs/cqrs'; |
2
|
|
|
import {Inject} from '@nestjs/common'; |
3
|
|
|
import {AddEventCommand} from './AddEventCommand'; |
4
|
|
|
import {ITaskRepository} from 'src/Domain/Task/Repository/ITaskRepository'; |
5
|
|
|
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository'; |
6
|
|
|
import {IEventRepository} from 'src/Domain/FairCalendar/Repository/IEventRepository'; |
7
|
|
|
import {IsMaximumTimeSpentReached} from 'src/Domain/FairCalendar/Specification/IsMaximumTimeSpentReached'; |
8
|
|
|
import {Event, EventType} from 'src/Domain/FairCalendar/Event.entity'; |
9
|
|
|
import {MaximumEventReachedException} from 'src/Domain/FairCalendar/Exception/MaximumEventReachedException'; |
10
|
|
|
import {IDateUtils} from 'src/Application/IDateUtils'; |
11
|
|
|
import {ProjectOrTaskMissingException} from 'src/Domain/FairCalendar/Exception/ProjectOrTaskMissingException'; |
12
|
|
|
import {AbstractProjectAndTaskGetter} from './AbstractProjectAndTaskGetter'; |
13
|
|
|
|
14
|
|
|
@CommandHandler(AddEventCommand) |
15
|
|
|
export class AddEventCommandHandler extends AbstractProjectAndTaskGetter { |
16
|
|
|
constructor( |
17
|
|
|
@Inject('ITaskRepository') taskRepository: ITaskRepository, |
18
|
|
|
@Inject('IProjectRepository') projectRepository: IProjectRepository, |
19
|
|
|
@Inject('IEventRepository') |
20
|
|
|
private readonly eventRepository: IEventRepository, |
21
|
|
|
@Inject('IDateUtils') |
22
|
|
|
private readonly dateUtils: IDateUtils, |
23
|
|
|
private readonly isMaximumTimeSpentReached: IsMaximumTimeSpentReached |
24
|
|
|
) { |
25
|
|
|
super(taskRepository, projectRepository); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public async execute(command: AddEventCommand): Promise<string> { |
29
|
|
|
const {type, date, projectId, taskId, summary, time, user} = command; |
30
|
|
|
|
31
|
|
|
if (type === EventType.MISSION && (!projectId || !taskId)) { |
32
|
|
|
throw new ProjectOrTaskMissingException(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
const project = await this.getProject(projectId); |
36
|
|
|
const task = await this.getTask(taskId); |
37
|
|
|
const event = new Event( |
38
|
|
|
type, |
39
|
|
|
user, |
40
|
|
|
time, |
41
|
|
|
this.dateUtils.format(date, 'y-MM-dd'), |
42
|
|
|
project, |
43
|
|
|
task, |
44
|
|
|
summary |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
if (true === (await this.isMaximumTimeSpentReached.isSatisfiedBy(event))) { |
48
|
|
|
throw new MaximumEventReachedException(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
const savedEvent = await this.eventRepository.save(event); |
52
|
|
|
|
53
|
|
|
return savedEvent.getId(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|